﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using MrDev.Graphics;
using MrDev.Physics;

namespace MrDev.GameObjects
{
    class Car : GameObject
    {
        private Animation animation;

        public Car(Level level, Vector2 position, int type, float randomTime)
            : base(level)
        {
            this.Level = level;
            Parameters = PhysicsParameters.CreateEnemyParameters();
            SetColor(Color.White);

            LoadContent(type);
            Level.Physics.SetObjectPosition(this, position);
            PlayAnimation(animation);
            bumpUpTime = randomTime;
        }

        private void LoadContent(int type)
        {
            // Load animated textures.
            animation = new Animation(Level.Content.Load<Texture2D>("Sprites/Enemy/Cars"), 0.1f, true, type, 1);
            animation.FrameWidth = 64;
            animation.FrameHeight = 32;
        }

        public override void Update(GameTime gameTime)
        {
            if (Parameters.Movement == 0)
                Parameters.Movement = -1;

            Rectangle bounds = Level.Physics.AbsoluteCollisionRect(this);
            int leftTile = (int)Math.Floor(((float)bounds.Left / Tile.Height));
            int rightTile = (int)Math.Floor(((float)bounds.Right / Tile.Height));
            int yTile = (int)Math.Floor(((float)bounds.Bottom / Tile.Height)) - 1;

            if (Parameters.Movement == -1)
            {
                TileCollision collisionLeft = Level.GetCollision(leftTile, yTile);
                TileCollision collisionLeftDown = Level.GetCollision(leftTile, yTile + 1);
                if (collisionLeftDown == TileCollision.Passable || collisionLeft == TileCollision.Impassable)
                    Parameters.Movement = 1;
            }
            else
            {
                TileCollision collisionRight = Level.GetCollision(rightTile, yTile);
                TileCollision collisionRightDown = Level.GetCollision(rightTile, yTile + 1);
                if (collisionRightDown == TileCollision.Passable || collisionRight == TileCollision.Impassable)
                    Parameters.Movement = -1;
            }

            Level.Physics.ApplyPhysics(this, gameTime);
            bool objectCollided = Level.Physics.HandleCollisions(this);
            if (objectCollided)
                Parameters.Movement *= -1;

        }

        float bumpUpTime = 0;
        float bumpDownTime = 0.25f;

        public override void Draw(GameTime gameTime, SpriteBatch spriteBatch)
        {
            float elapsedTime = (float)gameTime.ElapsedGameTime.TotalSeconds;
            bumpUpTime -= elapsedTime;
            bool bumpDown = bumpUpTime < 0;
            if (bumpDown)
                bumpDownTime -= elapsedTime;
            if (bumpDownTime < 0)
            {
                bumpDownTime = 0.25f;
                bumpUpTime = 0.4f;
            }


            if (bumpDown) this.Parameters.Position.Y += 2;
            base.Draw(gameTime, spriteBatch);
            if (bumpDown) this.Parameters.Position.Y -= 2;
        }


    }
}
